﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;

namespace vAddins
{
    public enum ReferenceType
    {
        System,
        Known,
        Absolute,
        Host
    }

    public class Reference
    {
        public ReferenceType Type { get; private set; }

        public string Path { get; private set; }

        internal Reference(ReferenceType type, string path)
        {
            Type = type;
            Path = path;
        }

        internal Reference(XmlNode e)
        {
            switch (e.Name)
            {
                case "SystemReference":
                    if (e.Attributes.Count != 1 || e.Attributes["path"] == null) throw new XmlException("All nodes within the \"References\" node must only have a \"path\" attribute.\r\nPlease look at the example manifest file in the vAddins assembly!");
                    if (e.InnerXml != string.Empty) throw new XmlException("All nodes within the \"References\" node must have no content (must be empty).\r\nPlease put the reference path in the \"path\" attribute.\r\nPlease look at the example manifest file in the vAddins assembly!");

                    Type = ReferenceType.System;
                    Path = e.Attributes["path"].Value;
                    break;
                case "KnownReference":
                    if (e.Attributes.Count != 1 || e.Attributes["path"] == null) throw new XmlException("All nodes within the \"References\" node must only have a \"path\" attribute.\r\nPlease look at the example manifest file in the vAddins assembly!");
                    if (e.InnerXml != string.Empty) throw new XmlException("All nodes within the \"References\" node must have no content (must be empty).\r\nPlease put the reference path in the \"path\" attribute.\r\nPlease look at the example manifest file in the vAddins assembly!");

                    Type = ReferenceType.Known;
                    Path = e.Attributes["path"].Value;
                    break;
                case "AbsoluteReference":
                    if (e.Attributes.Count != 1 || e.Attributes["path"] == null) throw new XmlException("All nodes within the \"References\" node must only have a \"path\" attribute.\r\nPlease look at the example manifest file in the vAddins assembly!");
                    if (e.InnerXml != string.Empty) throw new XmlException("All nodes within the \"References\" node must have no content (must be empty).\r\nPlease put the reference path in the \"path\" attribute.\r\nPlease look at the example manifest file in the vAddins assembly!");

                    Type = ReferenceType.Absolute;
                    Path = e.Attributes["path"].Value;
                    break;
                case "HostReference":
                    if (e.Attributes.Count != 1 || e.Attributes["path"] == null) throw new XmlException("All nodes within the \"References\" node must only have a \"path\" attribute.\r\nPlease look at the example manifest file in the vAddins assembly!");
                    if (e.InnerXml != string.Empty) throw new XmlException("All nodes within the \"References\" node must have no content (must be empty).\r\nPlease put the reference path in the \"path\" attribute.\r\nPlease look at the example manifest file in the vAddins assembly!");

                    Type = ReferenceType.Host;
                    Path = e.Attributes["path"].Value;
                    break;
                default:
                    throw new XmlException("Invalid tag identified within the vAddin's References node.\r\nPlease look at the example manifest file in the vAddins assembly!");
            }
        }
    }
}
